<?php
require_once (dirname(__FILE__) . '/functions.php');
#date related issues with Mamp & Mac & PHP 5.3
#date_default_timezone_set('America/Los_Angeles');
#due to this turn off warning for now:
#ini_set('display_errors', false);
$ziparray = $_POST['ziparray'];
$zipdir = $_POST['zipdir'];
$zip = new zipfile();
foreach ($ziparray as $file_name) {
  // If not an image
  if (strpos($file_name, 'images/') === FALSE) {
    $thisTemplate = str_replace('.php', '', $file_name);
    if ($file_name == 'style.css') {
      $file_content = tf_print_css_code(0);
    } elseif ($file_name == 'javascript.js') {
      $file_content = tf_print_js();
    } elseif ($file_name == 'functions.php') {
      $file_content = tf_create_functions_file();
    } elseif ($file_name == 'header.php') {
      $file_content = tf_create_header_file();
    } elseif ($file_name == 'footer.php') {
      $file_content = tf_create_footer_file();
    } elseif ($file_name == 'loop.php') {
      $file_content = tf_create_loop_file();
    } else {
      $file_content = tf_create_php_file($thisTemplate);
    }
    // If image
  } else {
    $filename = THFRPATH . 'uploads/' . str_replace('images/', '', $file_name);
    $fsize = @filesize($filename);
    $fh = fopen($filename, 'rb');
    $file_content = fread($fh, $fsize);
    fclose($fh);
  }
  $zip->addFile($file_content, $zipdir . '/' . $file_name);
}
$zipcontents = $zip->file();
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$zipdir.zip\"");
header("Content-length: " . strlen($zipcontents) . "\n\n");
echo $zipcontents;
die();
?>